# _*_ coding: utf-8 _*_
from __future__ import unicode_literals
import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
from mptt.models import MPTTModel, TreeForeignKey
import uuid
from django.utils import timezone

class Region(MPTTModel):
    """区域"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    parent = TreeForeignKey('self', related_name="children", verbose_name=u"父区域", null=True, blank=True)
    name = models.CharField(max_length=255, verbose_name=u"区域名称", default="")

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"区域信息"
        verbose_name_plural = verbose_name
        db_table = "tb_region"

    def __unicode__(self):
        return self.name


class Company(models.Model):
    """公司信息"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=128, verbose_name=u"公司名字", default="")
    logo = models.ImageField(upload_to="image/company/%Y/%m", default=u"image/company/default.png", max_length=100, verbose_name=u"公司logo", blank=True)
    domainName = models.CharField(max_length=128, verbose_name=u"域名", default="", blank=True)
    address = models.CharField(max_length=255, verbose_name=u"公司地址", default="")
    area_code = models.CharField(max_length=128, verbose_name=u"电话区号", default="", blank=True)
    description = models.CharField(max_length=255, verbose_name=u"公司描述", default="", blank=True)
    region = models.ForeignKey(Region, verbose_name=u"所属区域", related_name='companies', null=True, blank=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"公司信息"
        verbose_name_plural = verbose_name
        db_table = "tb_company"

    def __unicode__(self):
        return self.name

class UserJob(models.Model):
    """员工岗位"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=64, verbose_name=u"岗位名称", default="", unique=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"岗位信息"
        verbose_name_plural = verbose_name
        db_table = "tb_job"

    def __unicode__(self):
        return self.name


class Department(MPTTModel):
    """部门"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    parent = TreeForeignKey('self', related_name="children", verbose_name=u"父部门", null=True, blank=True)
    name = models.CharField(max_length=255, verbose_name=u"部门名称", default="")
    company = models.ForeignKey(Company, verbose_name=u"所属公司", related_name='departments', null=True, blank=True)
    jobs = models.ManyToManyField(UserJob, verbose_name=u"部门有效岗位", related_name='departments', db_table='re_depart_job')

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"部门信息"
        verbose_name_plural = verbose_name
        db_table = "tb_department"

    def __unicode__(self):
        return self.name




class UserProfile(AbstractUser):
    """用户信息表"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
    gender = models.CharField(max_length=6, choices=(("male", u"男"), ("female", u"女")), default="female",
                              verbose_name=u"性别")
    mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name=u"手机")
    avator = models.ImageField(upload_to="image/userprofile/%Y/%m", default=u"image/userprofile/default.png", max_length=100, verbose_name=u"头像", null=True, blank=True)
    isEnable = models.BooleanField(choices=((True, u"启用"), (False, u"禁用")), default=True, verbose_name=u"是否启用")
    description = models.CharField(max_length=255, verbose_name=u"员工简介", default="", blank=True)
    company = models.ForeignKey(Company, verbose_name=u"所属公司", null=True)
    departments = models.ManyToManyField(Department, verbose_name=u"所属部门", related_name='users', db_table='re_user_depart', blank=True)
    job = models.ForeignKey(UserJob, verbose_name=u"岗位", related_name='users', null=True, blank=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")


    class Meta:
        verbose_name = u"用户信息"
        verbose_name_plural = verbose_name
        db_table = "tb_user"

    def __unicode__(self):
        return self.nick_name

class SmsHist(models.Model):
    """短信验证历史记录"""
    mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name=u"手机")
    codestr = models.CharField(max_length=10, null=True, blank=True, verbose_name=u"验证码")
    timestamp = models.DateTimeField(u"发送时间", default=timezone.now)


class CustomerProfile(models.Model):
    """客户信息表"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=128, verbose_name=u"客户姓名", default="")
    gender = models.CharField(max_length=6, choices=(("male", u"男"), ("female", u"女")), default="female",
                              verbose_name=u"性别")
    mobile = models.CharField(max_length=11, null=True, blank=True, verbose_name=u"手机")
    avator = models.ImageField(upload_to="image/userprofile/%Y/%m", default=u"image/userprofile/default.png", max_length=100, verbose_name=u"头像", null=True, blank=True)
    address = models.CharField(max_length=255, verbose_name=u"地址", default="", null=True, blank=True)
    description = models.CharField(max_length=255, verbose_name=u"客户简介", default="", blank=True)
    sale = models.ForeignKey(UserProfile, verbose_name=u"营销", related_name='salecustomer', null=True, blank=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")


    class Meta:
        verbose_name = u"客户信息"
        verbose_name_plural = verbose_name
        db_table = "tb_customer"

    def __unicode__(self):
        return self.name



